package com.sugar.service.system.privilege;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sugar.base.query.PageModel;
import com.sugar.domain.system.privilege.PrivilegeResourceItem;
import com.sugar.domain.system.privilege.PrivilegeRole;
import com.sugar.persistence.system.privilege.IPrivilegeRole;

@Service
public class PrivilegeRoleServiceBean implements IPrivilegeRoleService{
	
	@Resource
	private IPrivilegeRole prole ;

	@Override
	public void deletePrivilegeRoleService(int id) {
		this.prole.deletePrivilegeRoleDao(this.loadPrivilegeRoleByIdService(id)) ;
		
	}

	@Override
	public void deleteSelectPrivilegeRoleService(Object[] ids) {
		this.prole.deleteSelectPrivilegeRoleDao(ids) ;
	}

	@Override
	public List<PrivilegeRole> findPrivilegeRoleService(Object[] param) {
		String queryString = "select o from PrivilegeRole o" ;
		
		return this.prole.findPrivilegeRoleDao(queryString, null, param, null) ;
	}

	@Override
	public PageModel<PrivilegeRole> getScorllPrivilegeRoleService(
			Object[] param, int firstPage, int pageSize) {
		String queryString = "select o from PrivilegeRole o" ;
		
		return this.prole.getScorllPrivilegeRoleDao(queryString, null, param, firstPage, pageSize, null) ;
	}

	@Override
	public PrivilegeRole loadPrivilegeRoleByIdService(int id) {
		
		return this.prole.loadPrivilegeRoleByIdDao(id) ;
	}

	@Override
	public void savePrivilegeRoleService(PrivilegeRole role) {
		this.prole.savePrivilegeRoleDao(role) ;
	}

	@Override
	public void updatePrivilegeRoleService(PrivilegeRole role,Object[] param) {
		
		PrivilegeRole pr = this.loadPrivilegeRoleByIdService(role.getR_id()) ;
		
		//给角色分配资源
		if(null != param && param.length > 0) {
			
			Set<PrivilegeResourceItem> resources = new HashSet<PrivilegeResourceItem>() ;
			
			String[] resids = param[0].toString().split(",") ;
			for(int i=0; i<resids.length; i++) {
				PrivilegeResourceItem ri = new PrivilegeResourceItem() ;
				ri.setMc_id(Integer.parseInt(resids[i])) ;
				resources.add(ri) ;
			}
			
			pr.setMres(resources) ;
			
			System.out.println(resources.size());
		}

		this.prole.updatePrivilegeRoleDao(pr) ;
	}

}
